package cn.edu.nju.askinginhand.util;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;

public class AudioRecorder{
	private File mSampleFile;
	private MediaRecorder mRecorder;

	private void initRecorder() {
		Log.v("AP","setAudioSource:2");
		//mRecorder.setAudioSource(/*MediaRecorder.AudioSource.MIC*/2);
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
		Log.v("AP","setOutputFormat:4");
		mRecorder.setOutputFormat(/*MediaRecorder.OutputFormat.RAW_AMR*/4);
		Log.v("AP","setAudioEncoder:2");
		mRecorder.setAudioEncoder(/*MediaRecorder.AudioEncoder.AMR_NB*/2);
		mSampleFile = null;
		try {
			//save as record.wav
			mSampleFile = File.createTempFile("record", ".wav", Environment
					.getExternalStorageDirectory());
			if(mSampleFile == null)
				System.out.println("Create file fail");
			mRecorder.setOutputFile(mSampleFile.getAbsolutePath());
			mRecorder.prepare();
		}catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void onRestartRecording() {
		// when plan comes to interrupt activity
		Log.i("Start Rec", "MessageManager");

		mRecorder = new MediaRecorder();
		initRecorder();
		mRecorder.start();
	}

	public void onStopRecording() {
		Log.i("stop rec", "MessageManager");

		if (mRecorder != null) {
			mRecorder.stop();
			mRecorder.release();
			mRecorder = null;
		}

	}

	public void onPauseRecording() {
		Log.i("pause rec", "MessageManager");
		mRecorder.reset();
	}

	public void onResumeRecording() {
		Log.i("resume rec", "MessageManager");
		mRecorder.start();
	}
	
	public File getFile() {
		return mSampleFile;
	}
}
